import Vue from 'vue'
import VueRouter from 'vue-router'

import Cart from '@/pages/Cart'
import Category from '@/pages/Category'
import Home from '@/pages/Home'
import User from '@/pages/User'

import CategoryList from '@/components/CategoryList' 
import ProductList from '@/components/ProductList'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
  },
  {
    path: '/category',
    name: 'Category',
    component: Category,
    children:[
      {
        path: '/categoryList',
        name: 'CategoryList',
        component:CategoryList 
      },
      {
        path: '/productList',
        name: 'ProductList',
        component:ProductList
      }
    ]
  },
  {
    path: '/cart',
    name: 'Cart',
    component: Cart,
  },
  {
    path: '/user',
    name: 'User',
    component: User,
  },
]

const router = new VueRouter({
  routes,
 
})

router.beforeEach((to, from, next) => {
  let isLogin = localStorage.getItem("isLogin");

  console.log("to:",to.path);
  console.log("from:",from.path)

  console.log("glob isLogin: ",isLogin);
  if (to.path === "/" || to.path==="/login" || to.path==="/category" ||  isLogin) {   
    console.log("to.path === /");
    next(); // 如果是登录路由或者已经是登录状态，放行   
  }else{
    console.log("进入其他页，未登录跳转首页！");
    window.location.href = "/";
  } 
})
export default router
